<?php

/**
 * @property CI_Loader $load
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_Email $email
 * @property CI_DB_active_record $db
 *
 */
class De extends CI_Controller {

    var $id;

    function __construct() {
        parent::__construct();
        $this->id = $this->session->userdata('id');
        $this->load->model("Newconnection");
        $this->load->model("Employee");
        $this->load->model("Applicant");
        $this->load->model("Customer");
        $this->load->model("Shiftconnection");
        $this->load->model("Complain");
        //  $this->load->model("Shifthandle");
        $this->load->helper('form');
    }

    function index() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_start_content";
        $data["sidebar"] = "admin/de/de_sidebar";
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array('de_start.js', 'sde_status.js');
        $this->load->view('template', $data);
    }

    function entrance() {

        if ($this->session->userdata('logged_in') != null && $this->session->userdata('rank') == 'DE') {
        {
            $this->id = $this->session->userdata('id');
            return 1;
            
        }
        } else {
            redirect('home');
        }
    }

    function new_recently_arrived() {
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_new_recent";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Newconnection->recently_arrived($this->id, 'Recent');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function DE() {
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_content";
        $data["sidebar"] = "admin/de/de_sidebar";
        $data["js"] = array("de.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function detail_ajax() {
        $id = $this->input->post('id');
        $data = json_encode($this->Applicant->get_Detail($id));
        echo $data;
    }

    function reject() {
        $id = $this->input->post('id');
        $backto = $this->input->post('from');
        $msg = $this->input->post('message');
        $this->Newconnection->update_status($id, 'Rejected');
        $this->Newconnection->update_message($appid, $msg);
        redirect('de/' . $backto);
    }

    function assign_sde() {
        $appid = $this->input->post('id');
        $empid = $this->input->post('sde');
        $code = $this->Employee->get_code($this->id);
        $this->load->model("Newhandle");

        $this->db->trans_start();
        $this->Newhandle->insert($appid, $empid, $code);
        $this->Newconnection->update_status($appid, 'Forwarded');
        $this->Newconnection->update_message($appid, 'Your applicatiion has been assigned a Sub Divisional Engineer for feasibility test');
        $this->db->trans_complete();

        redirect('de/new_recently_arrived');
    }

    function new_reported() {
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_new_reported";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Newconnection->recently_arrived($this->id, 'Reported');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_reported.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function result_ajax() {
        $id = $this->input->post('id');
        $data = json_encode($this->Applicant->get_Result($id));
        echo $data;
    }

    function issue_demand_note() {
        $appid = $this->input->post('id');

        $this->db->trans_start();
        $this->Newconnection->update_status($appid, 'Final');
        $this->Newconnection->update_message($appid, 'Download Demand Note and submit it to the bank and submit receipt to BTCL');
        $this->db->trans_complete();

        redirect('de/new_reported');
    }

    function new_final() {
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_new_final";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Newconnection->recently_arrived($this->id, 'Final');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_final.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function new_finalize() {
        // print_r($_POST);
        $id = $this->input->post('id');
        $day = $this->input->post('day');
        $phone = $this->input->post('phone');
        $data = $this->Applicant->get_Detail($id);
        $a_c = $data["AlreadyCustomer"];
        unset($data["AlreadyCustomer"]);

        $tel["phoneno"] = $phone;
        $tel["password"] = $phone;
        $tel["since"] = date("Y-m-d");
        $tel["address"] = $data["PresAdd"];
        $tel["code"] = $this->Employee->get_code($this->id);

        if ($a_c == "Yes") {
            unset($data["PresAdd"]);
            $this->db->where($data);
            $query = $this->db->get('customer');
            $row = $query->row();
            $cid = $row->Id;
        } else {
            unset($data["Id"]);
            $data["Since"] = date("Y-m-d");
            $data["Noofphones"] = 1;
        }

        //   print_r($data);

        $this->db->trans_start();
        $this->Newconnection->update_status($id, 'Old');
        $this->Newconnection->update_message($id, 'You will get your connection within ' . $day . ' days');
        if ($a_c == "Yes") {
            $this->db->set('Noofphones', 'Noofphones+1', false);
            $this->db->where('Id', $cid);
            $this->db->update('customer');
            $tel["Id"] = $cid;
        } else {
            $this->db->insert('customer', $data);
            $tel["Id"] = $this->db->insert_id();
        }
        $this->db->insert('telephone', $tel);
        $this->db->where('Id', $id);
        $this->db->delete('applicant');
        $this->db->trans_complete();
        redirect('de/new_final');
    }

    function new_old() {
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_new_old";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Newconnection->recently_arrived($this->id, 'Old');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_new_old.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function new_complete_ajax() {
        $appid = $this->input->post("appid");
        $date = date("Y-m-d");
        $this->db->set('accomplishdate', 'NOW()', false);
        $this->db->where('appid', $appid);
        $this->db->update('newconnection');
        echo $date;
    }

    /*     * *******************ShiftConnection************* */

    function shift_recently_arrived() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_shift_recent";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Shiftconnection->recently_arrived($this->id, 'Recent');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $this->load->model('Exchange');
        $data["exchange"] = $this->Exchange->exchange_list();
        $data["js"] = array("de_shift_recent.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function shift_detail_ajax() {
        $id = $this->input->post('id');
        $data = json_encode($this->Shiftconnection->get_Detail($id));
        echo $data;
    }

    function shift_assign_sde() {
        $appid = $this->input->post('id');
        $empid = $this->input->post('sde');
        $code = $this->Employee->get_code($this->id);
        $this->load->model("Shifthandle");

        $this->db->trans_start();
        $this->Shifthandle->insert($appid, $empid, $code);
        $this->Shiftconnection->update_status($appid, 'Forwarded');
        $this->Shiftconnection->update_message($appid, 'Your application has been assigned a Sub Divisional Engineer for feasibility test');
        $this->db->trans_complete();

        redirect('de/shift_recently_arrived');
    }

    function change_exchange() {
        $appid = $this->input->post('id');
        $code = $this->input->post('exchange');
        $this->load->model('Shifthandle');
        $this->Shifthandle->change_exchange($appid, $code);
        redirect('de/shift_recently_arrived');
    }

    function shift_reported() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_shift_reported";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Shiftconnection->recently_arrived($this->id, 'Reported');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $this->load->model('Exchange');
        $data["exchange"] = $this->Exchange->exchange_list();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_shift_reported.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function shift_result_ajax() {
        $id = $this->input->post('id');
        $data = json_encode($this->Shiftconnection->get_Result($id));
        echo $data;
    }

    function shift_reject() {
        $id = $this->input->post('id');
        $backto = $this->input->post('from');
        $msg = $this->input->post('message');
        $this->Shiftconnection->update_status($id, 'Rejected');
        $this->Shiftconnection->update_message($appid, $msg);
        redirect('de/' . $backto);
    }

    function shift_issue_demand_note() {
        $appid = $this->input->post('id');

        $this->db->trans_start();
        $this->Shiftconnection->update_status($appid, 'Final');
        $this->Shiftconnection->update_message($appid, 'Download Demand Note and submit it to the bank and submit receipt to BTCL');
        $this->db->trans_complete();

        redirect('de/shift_reported');
    }

    function shift_final() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_shift_final";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Shiftconnection->recently_arrived($this->id, 'Final');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_shift_final.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function shift_finalize() {
        $id = $this->input->post('id');
        $day = $this->input->post('day');
        $phoneno = $this->Shiftconnection->get_phone($id);
        $address = $this->Shiftconnection->get_newaddress($id);
        $this->db->trans_start();
        $this->Shiftconnection->update_status($id, 'Old');
        $this->Shiftconnection->update_message($id, 'You will get your connection within ' . $day . ' days');

        $this->db->set('address', $address);
        $this->db->where('phoneno', $phoneno);
        $this->db->update('telephone');

        $this->db->trans_complete();
        redirect('de/shift_final');
    }

    function shift_old() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_shift_old";
        $data["sidebar"] = "admin/de/de_sidebar";
        $query = $this->Shiftconnection->recently_arrived($this->id, 'Old');
        $data["list"] = $query->result_array();
        $data["count"] = $query->num_rows();
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_shift_old.js",'sde_status.js');
        $this->load->view('template', $data);
    }

    function shift_complete_ajax() {
        $date = date("Y-m-d");
        $appid = $this->input->post("appid");
        $this->db->set('accomplishdate', 'NOW()', false);
        $this->db->where('appid', $appid);
        $this->db->update('shiftconnection');
        $date = date("Y-m-d");
        echo $date;
    }

    /*     * *******************complain************* */

    function complain_recently_arrived() {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_complain_recent";
        $data["sidebar"] = "admin/de/de_sidebar";
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_complain_recent.js",'sde_status.js');

        $data["category"] = $this->Complain->get_category();
        $this->load->view("template", $data);
    }

    function get_complaints_ajax() {
        $category = $_POST['category'];
        $status = $_POST['status'];
        echo json_encode($this->Complain->get_complaints($this->id, $status, $category));
    }
    function complain_detail_ajax()
    {
        $id = $this->input->post('id');
        echo json_encode($this->Complain->get_complain_detail($id));
    }
    function complain_reject()
    {
        $id = $this->input->post('id');
        
        $this->db->trans_start();
        $this->Complain->update_status($id, 'Rejected');
        $this->Complain->update_message($id, 'Your Complain has been rejected for invalid information');
        $this->db->trans_complete();
        
        redirect('de/complain_recently_arrived' );
    }
    function complain_assign_sde()
    {
        $id = $this->input->post('id');
        $empid = $this->input->post('sde');
        $code = $this->Employee->get_code($this->id);
        
        $this->db->trans_start();
        $this->Complain->insert_handle($code, $empid, $id);
        $this->Complain->update_status($id, 'Undertaken');
        $this->Complain->update_message($id, 'Your applicatiion has been assigned a Sub Divisional Engineer to take necessary steps.');
         $this->db->trans_complete();
         
        redirect('de/complain_recently_arrived' );
    }
    function complain_undertaken()
    {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_complain_undertaken";
        $data["sidebar"] = "admin/de/de_sidebar";
        $data["status"] = "Undertaken";
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_complain_undertaken.js",'sde_status.js');
        $data["category"] = $this->Complain->get_category();
        $this->load->view("template", $data);        
    }
    function get_sde_status_ajax()
    {
        $empid=$_POST['empid'];
        echo json_encode($this->Employee->get_sde_status($empid));
    }
    function complain_solved()
    {
        $this->entrance();
        $data["title"] = "Divisional Engineer";
        $data["content"] = "admin/de/de_complain_undertaken";
        $data["sidebar"] = "admin/de/de_sidebar";
        $data["status"] = "Solved";
        $data["sde"] = $this->Employee->get_SDE($this->id);
        $data["js"] = array("de_complain_undertaken.js",'sde_status.js');
        $data["category"] = $this->Complain->get_category();
        $this->load->view("template", $data);     
    }
}

?>
